package com.hnkywl.sts.service.cwgl.kmys;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.cwgl.kmys.NbgskmdmysDao;
import com.hnkywl.sts.entity.cwgl.kmys.Nbgskmdmys;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-5-14
 * Time: 下午8:20
 * To change this template use File | Settings | File Templates.
 */
@Service
public class NbgskmdmysService {
    private NbgskmdmysDao nbgskmdmysDao;

    public String findNbgskmdmysList(Long syId,Long xyId,String khkmdm,String gyskmdm,int pageNo,int pageSize){
        Page<Map> page = nbgskmdmysDao.findAll(syId,xyId,khkmdm,gyskmdm,pageNo,pageSize);
        List result = page.getResult() != null ? page.getResult() : new ArrayList();
        return "{\"total\":"+page.getTotalCount()+",\"rows\":"+new Gson().toJson(result)+"}";
    }

    @Transactional
    public void save(Nbgskmdmys nbgskmdmys){
        if(nbgskmdmys.getSyId().longValue() == nbgskmdmys.getXyId().longValue())
            throw new RuntimeException("保存失败，上下游公司相同！");
        if(nbgskmdmysDao.checkUnique(nbgskmdmys))
            throw new RuntimeException("科目映射已存在！");
        nbgskmdmysDao.save(nbgskmdmys);
    }

    @Autowired
    public void setNbgskmdmysDao(NbgskmdmysDao nbgskmdmysDao) {
        this.nbgskmdmysDao = nbgskmdmysDao;
    }
}
